Ext.define('SistIntegral.Semovientes.FormVenta',{

alias			: "FrmVentaSemovientes",
forward			: true,
        
Formulario     :   function(btn){
btn.FLCmbProveedor = 'Cliente';
var CmbTipoDocumentoVenta = new SistIntegral.Componentes.Objetos().CmbTipoDocumentoVenta();
var ComboCPD = new SistIntegral.Componentes.Objetos().ComboCPD(btn);
var ComboProveedor = new SistIntegral.Componentes.Objetos().ComboProveedor(btn);
var ComboBanco = new SistIntegral.Componentes.Objetos().ComboBanco(btn);

var StorePlanE = Ext.create('Ext.data.Store', {
    model: 'VentaSemoviente',
    proxy: {
    type: 'ajax',
    url: 'ventasemoviente/IndexById/'+(btn.itemId=='edit' ? btn.Seleccion.get('IdVenta'):''),
    reader: {type: 'json',root: 'grid'}}
});

StorePlanE.on('load', function (store, records) {
    Ext.getCmp('FrmPlan').loadRecord(records[0]);
    
    ComboBanco.getStore().loadRecords(records); 
    ComboBanco.setValue(records[0].get('IdBanco'));
    
    CmbTipoDocumentoVenta.getStore().loadRecords(records); 
    CmbTipoDocumentoVenta.setValue(records[0].get('IdTipoDocumento'));    
    
    ComboCPD.getStore().loadRecords(records); 
    ComboCPD.setValue(records[0].get('IdProyecto')); 
    
    ComboProveedor.getStore().loadRecords(records); 
    ComboProveedor.setValue(records[0].get('IdProveedorCliente'));
    
});

var Frm =  Ext.create('Ext.form.Panel', {
    id:'FrmPlan',
    bodyPadding:'5',
    fieldDefaults: {
        msgTarget: 'side',
        labelAlign: 'right',
        labelWidth: 90,
        anchor: '100%'
        //,margin: '5 5 5 5'
    },
    items: [{
            xtype: 'container',
            anchor: '100%',
            layout: 'hbox',
            padding:'0 0 5 0',
            items: [CmbTipoDocumentoVenta,{
        xtype: 'datefield',
        flex: 1,
        fieldLabel: 'Fecha',
        name: 'FechaDocumento',
        allowBlank: false  // limited to the current date or prior
    }]
        },
        ComboCPD,{
            xtype: 'container',
            anchor: '100%',
            layout: 'hbox',
            padding:'0 0 5 0',
            items: [{
            xtype: 'radiogroup',
            fieldLabel: 'Tipo Pago',
            columns: 3,
            flex:1,
            items: [
                {boxLabel: 'Contado',id:'chbTipoPago', name: 'TipoPago', inputValue: 'C', checked: true},
                {boxLabel: 'Credito', name: 'TipoPago', inputValue: 'R'}
            ]
        },ComboBanco]
        },{
            xtype: 'container',
            anchor: '100%',
            layout: 'hbox',
            padding:'0 0 5 0',
            items: [{
            xtype: 'radiogroup',
            fieldLabel: 'Modalidad',
            columns: 3,
            flex:1,
            items: [
                {boxLabel: 'Interna', name: 'Modalidad', inputValue: 'I'},
                {boxLabel: 'Externa',id:'chbModalidad', name: 'Modalidad', inputValue: 'E', checked: true}
            ]
        }, {
                xtype:'textfield',
                fieldLabel: 'Nota Interna',
                id: 'NroNotaInterna',
                name: 'NroNotaInterna',
                disabled :true,
                allowBlank: true,
                flex:1,
                padding:'0 0 0 5'
            }]
        },{
                xtype:'textfield',
                fieldLabel: 'Voucher',
                name: 'NroVoucher',
                allowBlank: true,
                flex:1,
                padding:'0 0 0 5'
            },{
            xtype: 'container',
            anchor: '100%',
            layout: 'hbox',
            padding:'0 0 5 0',
            items: [{
            xtype: 'radiogroup',
            fieldLabel: 'Transferencia',
            columns: 2,
            flex:1,
            items: [
                {boxLabel: 'Titulo Gratuito', name: 'Transferencia', inputValue: 'S'},
                {boxLabel: 'Venta Normal', name: 'Transferencia', inputValue: 'N', checked: true}
            ]
        },{
            xtype: 'radiogroup',
            fieldLabel: 'Flag',
            columns: 2,
            flex:1,
            items: [
                {boxLabel: 'Produccion', name: 'FlagVentaAnimales', inputValue: 'P'},
                {boxLabel: 'Descarte', name: 'FlagVentaAnimales', inputValue: 'D', checked: true}
            ]
        }]
        },{xtype: 'hiddenfield',name: 'IdTipoVenta',value:3},
        {xtype: 'hiddenfield',name: 'Estado',value:'E'},
        ComboProveedor],
    buttons: [{
        text: 'Guardar',
        iconCls:'guardar',
        handler: function(){
            var form = this.up('form').getForm();
            if (form.isValid()) {
            form.submit({
                url: btn.hrefTarget+(btn.itemId == 'edit' ? btn.Seleccion.get('IdVenta'):''),
                success: function(form, action) {
                    Ext.getCmp('GridVentaSemoviente').getStore().load();
                    Ext.getCmp('WVentaSemoviente').close();
                    Ext.MessageBox.show({
                           msg: 'Registro Creado Correctamente...',
                           progressText: 'Actualizando...',
                           width:300,
                           icon:'ext-mb-download'
                       });
                        setTimeout(function(){
                            Ext.MessageBox.hide();
                        }, 1000); 
                },
                failure: function(form, action) {
                    //...
                }
            });
            }
        }
    },{
        text: 'Limpiar',
        iconCls:'limpiar',
        handler: function(){
            this.up('form').getForm().reset();
        }
    }]
});

Ext.getCmp('chbTipoPago').on('change', function (objeto, newValue, oldValue, eOpts) {
   //alert(newValue);
   if (newValue == true){
    ComboBanco.setDisabled(false);
   }
   else{
    ComboBanco.clearValue();
    ComboBanco.setDisabled(true); 
   }
});

Ext.getCmp('chbModalidad').on('change', function (objeto, newValue, oldValue, eOpts) {
   //alert(newValue);
   if (newValue == false){
    Ext.getCmp('NroNotaInterna').setDisabled(false);
   }
   else{
    Ext.getCmp('NroNotaInterna').setRawValue("");
    Ext.getCmp('NroNotaInterna').setDisabled(true); 
   }
});

    if(btn.itemId == 'edit' ){StorePlanE.load();}
    return Frm;
},        
        
Window      :   function(btn){
    var Seleccion;
        
    if(btn.itemId == 'edit'){
        Seleccion = Ext.getCmp('GridVentaSemoviente').getSelectionModel().getSelection()[0];
        if(!Seleccion){ 
        Ext.Msg.show({
            title: 'Advertencia',
            msg: 'Seleccione Registro de Venta a Editar',
            icon: Ext.MessageBox.WARNING,
            buttons: Ext.Msg.OK
        });
        return false;
        }
        else{
            btn.Seleccion = Seleccion
        }
    }

    var Item = this.Formulario(btn);

    var win = Ext.create('widget.window', {
    id:'WVentaSemoviente',
    title: btn.getText()+' Venta',
    closable: true,
    //bodyPadding:'5',
    border:false,
    width: 640,
    minWidth: 600,
    height: 270,
    layout:'fit',
    modal: true,
    items: [Item]
    });

    return win.show();
},

FormularioD     :   function(btn){

var CmbSemovienteFrm = new SistIntegral.Componentes.Objetos().CmbSemovienteFrm(btn);
var CmbMotivoVenta = new SistIntegral.Componentes.Objetos().CmbMotivoVenta(btn);


var StorePlanE = Ext.create('Ext.data.Store', {
    model: 'DetalleVenta',
    proxy: {
    type: 'ajax',
    url: 'detalleventa/IndexById/'+(btn.itemId=='edit' ? btn.Seleccion.get('IdDetalleVenta'):''),
    reader: {type: 'json',root: 'grid'}}
});

StorePlanE.on('load', function (store, records) {
    Ext.getCmp('FrmDetalleVenta').loadRecord(records[0]);
    
    CmbSemovienteFrm.getStore().loadRecords(records); 
    CmbSemovienteFrm.setValue(records[0].get('IdSemoviente'));
    
    CmbMotivoVenta.getStore().loadRecords(records); 
    CmbMotivoVenta.setValue(records[0].get('IdMotivoVenta'));
    
});

var Frm =  Ext.create('Ext.form.Panel', {
    id:'FrmDetalleVenta',
    bodyPadding:'5',
    fieldDefaults: {
        msgTarget: 'side',
        labelAlign: 'right',
        labelWidth: 90,
        anchor: '100%'
        //,margin: '5 5 5 5'
    },
    items: [CmbSemovienteFrm,{
            xtype: 'container',
            anchor: '100%',
            layout: 'hbox',
            padding:'0 0 5 0',
            items: [{
                xtype: 'numberfield',
                flex: 1,
                name: 'Cantidad',
                fieldLabel: 'Cantidad',
                value: 1,
                editable: false
            },{
                xtype: 'numberfield',
                flex: 1,
                fieldLabel: 'Precio',
                name: 'PrecioUnitario',
                allowBlank: false  // limited to the current date or prior
            }]
        }
//        ,{
//                xtype:'textfield',
//                fieldLabel: 'Glosa',
//                name: 'Glosa',
//                allowBlank: true,
//                flex:1
//            }
            ,CmbMotivoVenta,
            {xtype: 'hiddenfield',name: 'IdVenta',value:btn.record.get('IdVenta')}
        ],
    buttons: [{
        text: 'Guardar',
        iconCls:'guardar',
        handler: function(){
            var form = this.up('form').getForm();
            if (form.isValid()) {
            form.submit({
                url: btn.hrefTarget+(btn.itemId == 'edit' ? btn.Seleccion.get('IdDetalleVenta'):''),
                success: function(form, action) {
                    Ext.getCmp('GridDetalleVenta'+btn.record.get('IdVenta')).getStore().load();
                    Ext.getCmp('WDetalleVenta').close();
                    Ext.MessageBox.show({
                           msg: 'Registro Creado Correctamente...',
                           progressText: 'Actualizando...',
                           width:300,
                           icon:'ext-mb-download'
                       });
                        setTimeout(function(){
                            Ext.MessageBox.hide();
                        }, 1000); 
                },
                failure: function(form, action) {
                    //...
                }
            });
            }
        }
    },{
        text: 'Limpiar',
        iconCls:'limpiar',
        handler: function(){
            this.up('form').getForm().reset();
        }
    }]
});
    if(btn.itemId == 'edit' ){StorePlanE.load();}
    return Frm;
},

WindowD      :   function(btn){
    var Seleccion;
        
    if(btn.itemId == 'edit'){
        Seleccion = Ext.getCmp('GridDetalleVenta'+btn.record.get('IdVenta')).getSelectionModel().getSelection()[0];
        if(!Seleccion){ 
        Ext.Msg.show({
            title: 'Advertencia',
            msg: 'Seleccione Registro a Editar',
            icon: Ext.MessageBox.WARNING,
            buttons: Ext.Msg.OK
        });
        return false;
        }
        else{
            btn.Seleccion = Seleccion
        }
    }

    var Item = this.FormularioD(btn);

    return Ext.create('widget.window', {
    id:'WDetalleVenta',
    title: btn.getText()+' Detalle Venta Venta',
    closable: true,
    //bodyPadding:'5',
    border:false,
    width: 500,
    minWidth: 500,
    height: 180,
    layout:'fit',
    modal: true,
    items: [Item]
    }).show();
}

});